using System.Collections;
using System.Collections.Generic;
using BCW;
using BCWAccountVip;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using I2.Loc;
using UnityEngine;

public class ReputationProductRewardWindow : MonoBehaviour
{
	public static ReputationProductRewardWindow instance;

	[HideInInspector]
	public bool isActive;

	public Texture textureGold;

	public Texture textureCash;

	public Texture textureStar;

	[SerializeField]
	private GameObject buttonPrefab;

	[SerializeField]
	private GameObject window;

	[SerializeField]
	private UILabel killCount;

	[SerializeField]
	private UILabel deadCount;

	[SerializeField]
	private UILabel repCount;

	[SerializeField]
	private UILabel leaderTime;

	[SerializeField]
	private GameObject leaderTimeObject;

	[SerializeField]
	private Transform button;

	public Transform resultsHolder;

	public GameObject rewardScrollHolder;

	public UIScrollView rewardScroll;

	public UILabel titleLabel;

	public Transform textureHolder;

	public UIBasicSprite leagueTexture;

	public GameObject leagueUPAnim;

	public GameObject leagueDOWNAnim;

	public UILabel leagueName;

	public UISprite arrowSprite;

	public UILabel leagueUpLabel;

	public UISprite cupsBar;

	public UILabel cupsCount;

	private List<SessionReputationReward> rewardList;

	public GetawayResultsUI getawayResultsUI;

	private GameObject panelSaveReward;

	private bool _saveReward;

	[HideInInspector]
	public List<SessionReputationReward> testData;

	public UIScrollView productsScroll;

	public LeagueAvailableProduct newProductButton;

	[SerializeField]
	[Header("Sound Key")]
	private string keySoundReward;

	[SerializeField]
	private string keySoundLeagueUpCurLeague;

	[SerializeField]
	private string keySoundLeagueUpNextLeague;

	[SerializeField]
	private string keySoundLeagueDownCurLeague;

	[SerializeField]
	private string keySoundLeagueDownNextLeague;

	private List<Product> newProducts = new List<Product>();

	[HideInInspector]
	public int counter;

	private bool animationInProgress;

	[HideInInspector]
	public float moveAmount;

	private bool moveScrollInProgress;

	private void Awake()
	{
		instance = this;
		panelSaveReward = Object.Instantiate(Resources.Load<GameObject>("Prefabs/EndGameWindow"));
		panelSaveReward.transform.SetParent(base.transform);
		panelSaveReward.transform.localScale = Vector3.one;
		panelSaveReward.transform.localPosition = Vector3.zero;
		PanelEndGameRewardUI componentInChildren = panelSaveReward.GetComponentInChildren<PanelEndGameRewardUI>();
		_saveReward = componentInChildren.ApplySaveReward();
		panelSaveReward.gameObject.SetActive(_saveReward);
		if (_saveReward)
		{
			componentInChildren.IsActiveVip = ManagerVip.GetInstance().IsActiveVip();
			componentInChildren.Show();
			PanelEndGameRewardUI.giveReward += OnStart;
		}
	}

	private void Start()
	{
		if (!_saveReward)
		{
			OnStart();
		}
	}

	private void OnDestroy()
	{
		if (_saveReward)
		{
			PanelEndGameRewardUI.giveReward -= OnStart;
		}
	}

	public void OnStart()
	{
		int num = BCWStorage.Get<int>("GetawayResult");
		if (num != 0)
		{
			CupsManager.Cups += num;
			BCWStorage.Set("GetawayResult", 0);
			getawayResultsUI.Show(num);
		}
		rewardList = SessionReputationManager.Instance.GetLeagueReward();
		if (SessionReputationManager.Instance.savedListReward.Count != 0)
		{
			rewardList.AddRange(SessionReputationManager.Instance.savedListReward);
			SessionReputationManager.Instance.savedListReward.Clear();
		}
		if (rewardList.Count > 0 || CupsManager.StartGameLeague != CupsManager.CurrentLeague)
		{
			Show(rewardList);
		}
	}

	public string GetLeagueUpLabel()
	{
		string termTranslation = LocalizationManager.GetTermTranslation("Game_Common/lb_leagueUp");
		string arg = ScriptLocalization.Get("Game_Common/" + CupsManager.CurrentLeague) + " " + ScriptLocalization.Get("Game_Common/lobby_label_league");
		return string.Format(termTranslation, arg);
	}

	private void Show(List<SessionReputationReward> rewardList)
	{
		rewardScrollHolder.SetActive(rewardList.Count > 0);
		leagueTexture = leagueTexture.ReplaceForCurrentPlatform();
		isActive = true;
		window.SetActive(true);
		leagueTexture.LoadTextureAndAlphaMask("LigasIcons/", CupsManager.StartGameLeague.ToString());
		leagueName.text = ScriptLocalization.Get("Game_Common/" + CupsManager.CurrentLeague) + " " + ScriptLocalization.Get("Game_Common/lobby_label_league");
		if (CupsManager.StartGameLeague != CupsManager.CurrentLeague)
		{
			arrowSprite.gameObject.SetActive(true);
			titleLabel.gameObject.SetActive(false);
			leagueUpLabel.gameObject.SetActive(true);
			leagueUpLabel.text = GetLeagueUpLabel();
			cupsBar.fillAmount = CupsManager.GetLeagueFillAmount;
			cupsCount.text = CupsManager.Cups.ToString();
			if (CupsManager.StartGameLeague < CupsManager.CurrentLeague)
			{
				GetNewProducts();
				arrowSprite.color = Color.green;
				arrowSprite.transform.localRotation = Quaternion.Euler(0f, 0f, 180f);
				StartCoroutine(LeagueUPAnimation());
				titleLabel.text = string.Format(ScriptLocalization.Get("Game_Common/lb_leagueUp"), ScriptLocalization.Get("Game_Common/" + CupsManager.CurrentLeague));
			}
			else
			{
				arrowSprite.color = Color.red;
				arrowSprite.transform.localRotation = Quaternion.identity;
				StartCoroutine(LeagueDOWNAnimation());
				titleLabel.text = string.Format(ScriptLocalization.Get("Game_Common/lb_leagueDown"), ScriptLocalization.Get("Game_Common/" + CupsManager.CurrentLeague));
			}
		}
		else
		{
			arrowSprite.gameObject.SetActive(false);
			titleLabel.gameObject.SetActive(true);
			leagueUpLabel.gameObject.SetActive(false);
			titleLabel.text = ScriptLocalization.Get("Game_Common/lb_results");
			StartCoroutine(SetCountLabel(cupsCount, CupsManager.Cups, CupsManager.Cups - CupsManager.SesionCupsCount));
			cupsBar.fillAmount = CupsManager.GetLeagueFillAmount - CupsManager.GetSessionLeagueFillAmount;
			HOTween.To(cupsBar, 1f, new TweenParms().Prop("fillAmount", CupsManager.GetLeagueFillAmount));
		}
		StartCoroutine(ShowRewardWithDelay(rewardList));
		if (rewardList.Count == 0 && newProducts.Count == 0)
		{
			resultsHolder.localPosition = Vector3.up * -200f;
		}
		else
		{
			resultsHolder.localPosition = Vector3.zero;
		}
	}

	public void GiveReward()
	{
		SessionReputationManager.Instance.GiveReward(button.position, rewardList);
		CupsManager.StartGame();
		isActive = false;
		window.SetActive(false);
	}

	private IEnumerator ShowRewardWithDelay(List<SessionReputationReward> rewardList)
	{
		while (animationInProgress)
		{
			yield return null;
		}
		if (rewardList.Count > 0)
		{
			rewardScrollHolder.SetActive(true);
		}
		rewardScroll.ResetPosition();
		rewardScroll.enabled = false;
		for (int i = 0; i < rewardList.Count; i++)
		{
			ReputationProductReward reward = Object.Instantiate(buttonPrefab).GetComponentInChildren<ReputationProductReward>();
			reward.transform.SetParent(buttonPrefab.transform.parent);
			reward.transform.localScale = Vector3.one;
			reward.transform.localPosition = new Vector3(190 * i, buttonPrefab.transform.localPosition.y, 0f);
			reward.Set(rewardList[i]);
			reward.gameObject.SetActive(true);
			BCWAudioResource.Instance.PlaySound(keySoundReward);
			if (rewardList[i].upLeague)
			{
				reward.backgroundLeague.SetActive(true);
			}
			if (i > 3)
			{
				StartCoroutine(MoveScroll(190f, rewardScroll));
			}
			yield return new WaitForSeconds(0.3f);
		}
		rewardScroll.enabled = true;
	}

	private IEnumerator SetProductsFor()
	{
		if (newProducts.Count > 0)
		{
			while (animationInProgress)
			{
				yield return null;
			}
			if (rewardList.Count == 0)
			{
				productsScroll.transform.parent.localPosition = rewardScrollHolder.transform.localPosition;
			}
			productsScroll.transform.parent.gameObject.SetActive(true);
			productsScroll.ResetPosition();
			for (int i = 0; i < newProducts.Count; i++)
			{
				LeagueAvailableProduct button = Object.Instantiate(newProductButton).GetComponent<LeagueAvailableProduct>();
				button.transform.SetParent(newProductButton.transform.parent);
				button.transform.localScale = Vector3.one;
				button.transform.localPosition = new Vector3(190 * i, newProductButton.transform.localPosition.y, 0f);
				button.gameObject.SetActive(true);
				button.SetInfo(newProducts[i]);
			}
		}
	}

	private void GetNewProducts()
	{
		foreach (Product value in MarketController.Instance.allProducts.Values)
		{
			if (value.availableForBuy && !value.Purchased && (value.nextProduct == null || !value.nextProduct.Purchased) && (value.prevProduct == null || value.Purchased) && value.tier == CupsManager.CurrentTier && !(value is ProductVIP))
			{
				newProducts.Add(value);
			}
		}
	}

	private IEnumerator SetCountLabel(UILabel label, int count, int startCount = 0)
	{
		while (animationInProgress)
		{
			yield return null;
		}
		if (count < 2)
		{
			label.text = count.ToString();
			yield break;
		}
		counter = startCount;
		animationInProgress = true;
		UILabel label2 = default(UILabel);
		HOTween.To(this, 1f, new TweenParms().Prop("counter", count).OnUpdate((TweenDelegate.TweenCallback)delegate
		{
			label2.text = counter.ToString();
		}).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			BCWAudioResource.Instance.Stop("button_02");
			animationInProgress = false;
		}));
		yield return new WaitForSeconds(1.5f);
		animationInProgress = false;
	}

	private IEnumerator MoveScroll(float newMoveAmount, UIScrollView scroll)
	{
		while (moveScrollInProgress)
		{
			yield return null;
		}
		float delta = 0f;
		float prevMoveAmount = 0f;
		moveAmount = 0f;
		moveScrollInProgress = true;
		UIScrollView scroll2 = default(UIScrollView);
		HOTween.To(this, 0.1f, new TweenParms().Prop("moveAmount", newMoveAmount).Ease(EaseType.EaseInOutExpo).OnUpdate((TweenDelegate.TweenCallback)delegate
		{
			delta = moveAmount - prevMoveAmount;
			prevMoveAmount = moveAmount;
			scroll2.MoveRelative(new Vector3(0f - delta, 0f, 0f));
		})
			.OnComplete((TweenDelegate.TweenCallback)delegate
			{
				moveScrollInProgress = false;
			}));
	}

	private IEnumerator LeagueUPAnimation()
	{
		leagueUPAnim.SetActive(true);
		yield return new WaitForSeconds(0.75f);
		BCWAudioResource.Instance.PlaySound(keySoundLeagueUpCurLeague);
		HOTween.To(leagueTexture, 1f, new TweenParms().Prop("alpha", 0).Ease(EaseType.EaseInOutExpo));
		BCWDebug.LogError("Start");
		HOTween.To(textureHolder, 1.5f, new TweenParms().Prop("localScale", Vector3.one * 0.15f).Ease(EaseType.EaseInOutExpo).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			leagueTexture.LoadTextureAndAlphaMask("LigasIcons/", CupsManager.CurrentLeague.ToString());
			textureHolder.localScale = Vector3.one * 1.5f;
			HOTween.To(leagueTexture, 0.6f, new TweenParms().Prop("alpha", 1).Ease(EaseType.EaseInOutExpo).OnComplete((TweenDelegate.TweenCallback)delegate
			{
				BCWAudioResource.Instance.PlaySound(keySoundLeagueUpNextLeague);
			}));
			HOTween.To(textureHolder, 1.5f, new TweenParms().Prop("localScale", Vector3.one).Ease(EaseType.EaseInOutElastic).OnComplete((TweenDelegate.TweenCallback)delegate
			{
				BCWDebug.LogError("END");
				StartCoroutine(SetProductsFor());
			}));
		}));
	}

	private IEnumerator LeagueDOWNAnimation()
	{
		leagueDOWNAnim.SetActive(true);
		bool startAfterDelay = false;
		yield return new WaitForSeconds(0.75f);
		BCWAudioResource.Instance.PlaySound(keySoundLeagueDownCurLeague);
		HOTween.To(leagueTexture, 1.5f, new TweenParms().Prop("alpha", 0).Ease(EaseType.EaseInOutExpo));
		HOTween.To(textureHolder, 2f, new TweenParms().Prop("localScale", Vector3.one * 2f).Ease(EaseType.EaseInOutExpo).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			leagueTexture.LoadTextureAndAlphaMask("LigasIcons/", CupsManager.CurrentLeague.ToString());
			textureHolder.localScale = Vector3.one * 0.15f;
			HOTween.To(leagueTexture, 2f, new TweenParms().Prop("alpha", 1).Ease(EaseType.EaseInOutElastic).OnUpdate((TweenDelegate.TweenCallback)delegate
			{
				if ((double)leagueTexture.alpha >= 0.3 && !startAfterDelay)
				{
					BCWAudioResource.Instance.PlaySound(keySoundLeagueDownNextLeague);
					startAfterDelay = true;
				}
			}));
			HOTween.To(textureHolder, 2f, new TweenParms().Prop("localScale", Vector3.one).Ease(EaseType.EaseInOutElastic).OnComplete((TweenDelegate.TweenCallback)delegate
			{
			}));
		}));
	}
}
